package com.irdstudio.efp.esb.api.rest.sed.ploaninfo;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.efp.console.service.facade.PubSysInfoService;
import com.irdstudio.efp.esb.api.bean.sed.ploaninfo.YedReqLoanInfoQuery;
import com.irdstudio.efp.esb.api.bean.sed.ploaninfo.YedRespLoanInfoQuery;
import com.irdstudio.efp.esb.api.common.EsbRespServiceBeanCreator;
import com.irdstudio.efp.esb.api.service.sed.ploaninfo.YedLoanInfoQueryParams;
import com.irdstudio.efp.esb.api.service.sed.ploaninfo.YedLoanInfoQueryService;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.constant.EsbSysHeadEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;


/**
 * 优e贷已结清、未结清借据查询
 *
 * @author
 * @version 0.1
 * @date 2020/11/12 09:39
 */
@RestController
@RequestMapping("/esb")
public class YedLoanInfoQueryController implements FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(YedLoanInfoQueryController.class);

    @Autowired
    @Qualifier("yedLoanInfoQueryService")
    private YedLoanInfoQueryService yedLoanInfoQueryService;

    @Autowired
    private PubSysInfoService pubSysInfoService;

    /**
     * 获取ESB请求对象，校验 ESB请求系统头和请求应用头对象
     *
     * @param request 请求对象集合
     * @return
     */
    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("校验 ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }

    /**
     * 获取ESB请求对象，校验 ESB请求体对象
     *
     * @param request 请求对象集合
     * @return
     */
    @ModelAttribute("yedReqLoanInfoQuery")
    public YedReqLoanInfoQuery getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("校验 ESB请求体对象！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, YedReqLoanInfoQuery.class, Feature.InternFieldNames);
    }

    /**
     * 处理请求
     *
     * @param esbReqServiceBean   esb请求对象
     * @param br1
     * @param yedReqLoanInfoQuery 返回对象
     * @param br2
     * @param exception
     * @return
     */
    @PostMapping("/SERVICE_30230006_06")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult br1,
                                     @Validated @ModelAttribute("yedReqLoanInfoQuery") YedReqLoanInfoQuery yedReqLoanInfoQuery, BindingResult br2, Exception exception) {
        String glblSrlNo = esbReqServiceBean.getSysHead().getGlblSrlNo();
        logger.info("借据信息30230006_06查询开始,全局流水号为：[{}]",glblSrlNo);
        //查询类型
        String qryType = yedReqLoanInfoQuery.getQryTp();
        //页码-当前查询第几页的数据
        String pageNo = yedReqLoanInfoQuery.getPgNO();
        //每页显示条数
        String pageSize = yedReqLoanInfoQuery.getPerPgDsplCnt();

        //响应体
        YedRespLoanInfoQuery yedRespLoanInfoQuery = new YedRespLoanInfoQuery();

        if (Objects.nonNull(exception)) {
            //如果异常不为空，那么处理异常返回信息
            return yedLoanInfoQueryService.getEsbRespServiceBean(esbReqServiceBean, exception, pageNo, pageSize, yedRespLoanInfoQuery, EsbRetCodeStandard.JKYDCSJYBTG);
        }

        try {

            String openDay = pubSysInfoService.getOpenday("1001");
            if (StringUtils.isEmpty(openDay)) {
                throw new Exception("获取openday为空");
            }

            //根据查询类型封装不同条件的查询参数
            YedLoanInfoQueryParams params = yedLoanInfoQueryService.transferQueryParams(qryType, yedReqLoanInfoQuery);

            //调用互金并处理结果返回
            yedRespLoanInfoQuery = yedLoanInfoQueryService.getLoanInfo(params,qryType,yedReqLoanInfoQuery,glblSrlNo);

            //int totalSize = yedRespLoanInfoQuery.getMsgInfoArryList().size();

            //logger.info("借据信息查询接口---开始处理要返回的数据");
            //yedLoanInfoQueryService.setRespPersonalLoanInfo(pageNo, pageSize, yedRespLoanInfoQuery, totalSize);
            //logger.info("借据信息查询接口---数据处理结束");

            yedRespLoanInfoQuery.setRetCd(ESBConstant.SUCCESS);
            yedRespLoanInfoQuery.setRetMsg("查询成功");
            //返回报文
            return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                    JSONObject.toJSONString(yedRespLoanInfoQuery, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero),
                    EsbSysHeadEnums.RetStEnum.SUCCESS.getCode(),
                    EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(),
                    "查询成功");
        } catch (BizException ex) {
            logger.error("借据信息30230006_06查询接口出现异常，异常信息为：[{}]", ex.getMessage());
            ex.printStackTrace();
            return yedLoanInfoQueryService.getEsbRespServiceBean(esbReqServiceBean, ex, pageNo, pageSize, yedRespLoanInfoQuery, EsbRetCodeStandard.JKYDCSJYBTG);
        } catch (Exception e) {
            logger.error("借据信息30230006_06查询接口出现异常，异常信息为：[{}]", e.getMessage());
            e.printStackTrace();
            return yedLoanInfoQueryService.getEsbRespServiceBean(esbReqServiceBean, e, pageNo, pageSize, yedRespLoanInfoQuery, EsbRetCodeStandard.SJCLYC);

        }
    }
}
